package com.jsong.linuo.array;


import java.util.Random;
import java.util.Scanner;

/**
 * 数组练习:
 * 1.获取最大值
 * 2.统计符合条件的数据个数
 * 3.按照指定格式输出元素
 * 4.统计偶数个数
 * 5.查找元素
 * 6.数组复制
 * 7.数组扩容
 * 8.数组合并
 */
public class ArrayTest {
    public static void main(String[] args) {
        //练习1
        //求出数组中元素的最大值
        //元素和元素之间做比较,每次取比较大的,比到最后,得出最大值
        int[] arr1 = {3, 7, 1, 6, 8, 4, 9, 3};
        int max = arr1[1];
        for (int i = 1; i < arr1.length; i++) {
            if (max > arr1[i]) {
                max = arr1[i];
            }
        }
        System.out.println("max = " + max);

        //练习2
        //随机产生10个[0,100]之间的整数,统计既是3又是5,但不是7的倍数的个数

        //1.创建Random对象
        Random rd = new Random();
        //2.定义一个数组,长度为10
        int[] arr2 = new int[10];
        //3.定义一个变量count,统计个数
        int count = 0;
        //4.将随机生成的数存入数组中
        for (int i = 0; i < arr2.length; i++) {
            arr2[i] = rd.nextInt(101);
        }
        //遍历数组,条件判定
        for (int i = 0; i < arr2.length; i++) {
            if (arr2[i] % 3 == 0 && arr2[i] % 5 == 0 && arr2[i] % 7 != 0) {
                count++;
            }
        }
        System.out.println("count = " + count);

        //练习3
        //需求:定义一个数组 arr3 = {1,2,3,4} 输出按照[1,2,3,4]输出
        int[] arr3 = {1, 2, 3, 4};
        System.out.print("[");
        for (int i = 0; i < arr3.length; i++) {
            if (i == arr3.length - 1) {
                System.out.println(arr3[i] + "]");
            } else {
                System.out.print(arr3[i] + ",");
            }
        }

        //练习4
        //需求:随机50个1-100之间的整数,统计偶数个数

        int[] arr4 = new int[50];
        int count1 = 0;
        for (int i = 0; i < arr4.length; i++) {
            arr4[i] = rd.nextInt(100) + 1;
            if (arr4[i] % 2 == 0) {
                count1++;
            }
        }
        System.out.println("count1 = " + count1);

        //练习5
        //键盘录入一个整数,找出整数在数组中存储的索引位置
        //如果没有对应值则输出 不存在
        Scanner sc = new Scanner(System.in);
        int data = sc.nextInt();

        int[] arr5 = {11, 6, 3, 8, 24, 64};
        int flat = 0;//标记是否有匹配的数

        for (int i = 0; i < arr5.length; i++) {
            if (data == arr5[i]) {
                System.out.println("索引值为" + i);
                flat++;
            }
        }
        if (flat == 0) {
            System.out.println("该数不存在!");
        }

        //练习6 数组复制
        int[] arr6 = new int[6];
        for (int i = 0; i < arr5.length; i++) {
            arr6[i] = arr5[i];
        }

        //练习7 数组扩容
        //定义一个数组arr7 = {1,2,3,4,5} 由原来的长度扩容到10
        //创建一个指定长度的新数组,将新数组的地址给旧数组

        int[] oldArr = {1, 2, 3, 4, 5};
        int[] newArr = new int[10];

        for (int i = 0; i < oldArr.length; i++) {
            newArr[i] = oldArr[i];
        }
        //新数组的地址值给旧数组
        oldArr = newArr;
        for (int i = 0; i < oldArr.length; i++) {
            System.out.print(oldArr[i] + " ");
        }
        System.out.println();

        //练习8 数组合并
        //创建新数组长度为俩数组合并长度
        int[] arr8 = {1, 2, 3};
        int[] arr9 = {4, 5, 6};
        int[] newArr1 = new int[arr8.length + arr9.length];

        for (int i = 0; i < arr8.length; i++) {
            newArr1[i] = arr8[i];
        }

        //引入中间值
        int len = arr8.length;//3

        for (int i = 0; i < arr9.length; i++) {
            newArr1[i + len] = arr9[i];
        }

        for (int i = 0; i < newArr1.length; i++) {
            System.out.print(newArr1[i] + " ");
        }


    }
}
